#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-
import  aipai,kuaishou,lishiping,meipai,miaopai
import requests,random
import os,re,IPy,time


def writeMail(name, res):  # 写入到log
    if(res[1]!='网内' and res[0]!='sjycdn.miaopai.com'):#视界云无资源，重定向到网宿和网信缓存
        fmail = open('/tmp/ShortV.txt', 'a')
        fmail.write(t + '\t' + name + '\t%s\t%s\t%s\n' % res)
        fmail.close()
        global mailDefault
        mailDefault=True #出现非网内结果时，修改全局变量，将发送邮件改为是


def sendMail():#邮件告警
    #/bin/mail -s "短视频检测到出网" ganlian@watone.com.cn  < /tmp/ShortV.txt
    os.system('/bin/mail -s "短视频检测到出网" ganlian@watone.com.cn  < /tmp/ShortV.txt')
    os.system('/bin/mail -s "短视频检测到出网" songhp@watone.com.cn  < /tmp/ShortV.txt')




def web(hname,wname,res):#写入到网页
    fweb = open('/var/www/html/short_video/%s.html' % hname, 'a')
    fweb.write('<tr><td>' + t + '</td><td>'+wname+'</td><td>%s</td><td>%s</td><td>%s</td></tr>' % res)
    fweb.close()

def log(name,res):#写入到log
    fLog = open('/var/www/html/short_video/log/%sShortVideoLog.txt' % time.strftime("%Y-%m-%d", time.localtime()), 'a')
    fLog.write(t + '\t'+name+'\t%s\t%s\t%s\n' % res)
    fLog.close()



if __name__ == '__main__':
    #爱拍调度url
    AIPAI_URL = 'http://m.aipai.com/mobile/apps/apps_module-auxplayer_func-indexDataNew_os-1_assetId-46631848.html'
    #秒拍资源url，最后一级的字段替换到调度url中去请求
    MIAOPAI_URL = "http://www.miaopai.com/show/edcgx2yW3~cDhuclzXaU7jWQPOqlRPNNZ9W3tQ__.htm"
    #美拍调度url
    MEIPAI_URL='https://api.meipai.com/hot/feed_timeline.json?page=1&guid=990cb3dfb9d00c2ba105a3ceeddd5e6a&language=zh-Hans&client_id=1089857302&device_id=359654060840700&version=6300&channel=qq_cpd&model=SM-G9250&os=6.0.1&origin_channel=qq_cpd&locale=1&iccid=89860086111551164443&imei=359654060840700&mac=02%3A00%3A00%3A00%3A00%3A00&android_id=a5f6071cd589fc61&ab_codes=%5B0%5D&sig=03cc322b814336060dfa83da638df1e3&sigVersion=1.3&sigTime=1503036522800'

    #梨视频调度url
    LISHIPIN_URL = 'http://app.pearvideo.com/clt/jsp/v3/getVodConts.jsp?isHome=1&channelCode=&start=30'
    #快手调度url，post的url，post的传递的内容data，猜测和sig这个字段有关，替换测试未成功
    KS_sig = '1f7dd1678c42cb47f75ea043ecf14cec'
    KS_DATA = {'type': '7',
               'page': '5',
               'coldStart': 'false',
               'count': '20', 'pv':
                'false', 'id': '8',
                'refreshTimes': '7',
               'pcursor': '1',
               'client_key': '3c2cd3f3',
               'os': 'android',
                'sig': KS_sig
               }
    KUAISHOU_URL = 'http://180.186.38.200/rest/n/feed/hot?mod=samsung(SM-G9250)&lon=120.096915&country_code=CN&did=ANDROID_a5f6071cd589fc61&app=0&net=WIFI&oc=UNKNOWN&ud=0&c=MYAPP_CPD&sys=ANDROID_6.0.1&appver=5.2.1.4701&ftt=&language=zh-cn&lat=30.26885&ver=5.2'

    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    #初始化列表，用于存储运行的结果
    AIPAI_List=[]
    MIAOPAI_List=[]
    MEIPAI_List=[]
    LISHIPIN_List=[]
    KUAISHOU_List=[]
    #清空告警邮件文本
    fmial = open('/tmp/ShortV.txt', 'w')
    fmial.close()
    #是否发告警邮件,默认为否
    mailDefault=False

    #各请求调度10次，结果存在列表中
    for i in range(10):
        #assetId生成随机数去请求调度获得不通的资源url
        RAMD = str(random.randint(46631000, 46631999))
        AIPAI_URL = 'http://m.aipai.com/mobile/apps/apps_module-auxplayer_func-indexDataNew_os-1_assetId-%s.html' % RAMD
        AIPAI_res = aipai.main(AIPAI_URL)
        AIPAI_List.append(AIPAI_res)
        #
        MIAOPAI_res=miaopai.main(MIAOPAI_URL)
        MIAOPAI_List.append(MIAOPAI_res)

        MEIPAI_res=meipai.main(MEIPAI_URL)
        MEIPAI_List.append(MEIPAI_res)

        LISHIPIN_res=lishiping.main(LISHIPIN_URL)
        LISHIPIN_List.append(LISHIPIN_res)

        KUAISHOU_res=kuaishou.main(KUAISHOU_URL,KS_DATA)
        KUAISHOU_List.append(KUAISHOU_res)
    #写html表开始标签
    for i in ['aipai','miaopai','meipai','lishipin','kuaishou']:
        fweb = open('/var/www/html/short_video/%s.html' % i, 'w')
        fweb.write('<table>')
        fweb.write('<th width=15%>检测时间</th><th width=5%>短视频</th><th width=20%>资源域名</th><th width=5%>命中</th><th>IP</th>')
        fweb.close()

#写入日志、邮件和网页
    for res  in  AIPAI_List:
        print('%s\t%s\t%s\n' % res)
        log('爱拍', res)
        writeMail('爱拍', res)
        web('aipai','爱拍',res)
    for res  in  MIAOPAI_List:
        print('%s\t%s\t%s\n' % res)
        log('秒拍', res)
        writeMail('秒拍', res)
        web('miaopai', '秒拍',res)
    for res  in  MEIPAI_List:
        print('%s\t%s\t%s\n' % res)
        log('美拍', res)
        writeMail('美拍', res)
        web('meipai', '美拍',res)
    for res  in  LISHIPIN_List:
        print('%s\t%s\t%s\n' % res)
        log('梨视频', res)
        writeMail('梨视频', res)
        web('lishipin', '梨视频',res)
    for res  in  KUAISHOU_List:
        print('%s\t%s\t%s\n' % res)
        log('快手', res)
        writeMail('快手', res)
        web('kuaishou', '快手',res)
#写html封闭标签
    for i in ['aipai','miaopai','meipai','lishipin','kuaishou']:
        fweb = open('/var/www/html/short_video/%s.html' % i, 'a')
        fweb.write('</table>')
        fweb.close()
    #if mailDefault:
        #sendMail()





